<template>
  <div class="yd-option" v-show="filterShow">
    <div @click="handleSelect">
      <slot>{{label}}</slot>
    </div>
  </div>
</template>

<script>
import { getCurrentInstance, ref, watch } from "vue";
export default {
  name: "yd-option",
  props: {
    label: [String, Number],
    value: [String, Number]
  },
  setup(props, ctx) {
    let filterShow = ref(true);
    const instance = getCurrentInstance();
    const myParentCtx = instance.parent.parent.parent.ctx;
    const handleSelect = () => {
      let data = {
        label: props.label,
        value: props.value
      };
      myParentCtx.handleSetValue(data);
    };
    watch(
      () => myParentCtx.filterKeyword,
      val => {
        if (!val) {
          filterShow.value = true;
          myParentCtx.handleSetOptionCount(true);
          return false;
        }
        if (myParentCtx.filterable) {
          if (props.label.indexOf(val) >= 0) {
            filterShow.value = true;
            myParentCtx.handleSetOptionCount(true);
          } else {
            filterShow.value = false;
            myParentCtx.handleSetOptionCount(false);
          }
        }
      }
    );

    return {
      filterShow,
      handleSelect
    };
  }
};
</script>

<style lang="scss" scoped>
.yd-option {
  min-height: 36px;
  height: 36px;
  line-height: 36px;
  padding: 0 20px;
  box-sizing: border-box;
  color: #666;
  font-size: 14px;
  transition: all 0.2s;
}
.yd-option:hover {
  background-color: #f5f7fa;
}
</style>